let obj = require("../../assets/js/my.js");
//引入axios
const axios = require("../../assets/js/axios.js");
// require("../../assets/js/map.js");
// 引入字体图标样式;
require("../../assets/fonts/iconfont.css");

let $ = obj.$;
let formatTime = obj.formatTime;
let getRandom = obj.getRandom;
let load = obj.load;
// 引入自己的样式;
require("./countdown.less");
load(async function () {
	// var p1 = [116.434027, 39.941037];
	// var p2 = [116.461665, 39.941564];
	// // 返回 p1 到 p2 间的地面距离，单位：米
	// var dis = AMap.GeometryUtil.distance(p1, p2);
	// console.log(dis);
	// 定义数字数组;
	distant = function () {
		return new Promise((reslove, reject) => {
			AMap.plugin("AMap.Geolocation", function () {
				var geolocation = new AMap.Geolocation({
					enableHighAccuracy: true, // 是否使用高精度定位，默认：true
					timeout: 10000, // 设置定位超时时间，默认：无穷大
					offset: [10, 20], // 定位按钮的停靠位置的偏移量
					zoomToAccuracy: true, //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
					position: "RB", //  定位按钮的排放位置,  RB表示右下
				});
				// let ss = {
				// 	name: "学习",
				// };
				geolocation.getCurrentPosition(function (status, result) {
					if (status == "complete") {
						reslove(result);
					} else {
						alert("定位失败");
					}
				});
			});
		});
	};
	let s = null;
	let data = null;
	let p1 = null;
	do {
		s = await distant();
		data = s.position;
		p1 = [data.KL, data.kT];
	} while (s.info === "SUCCESS");

	let p2 = [];
	let dis = 0;
	let index1 = 0;
	let meter = 0;
	let calorie = 0;
	// 运动即使
	let runId = null;
	// let arr = [3, 2, 1, "READY", "Go"];
	// // 计数
	// let index = 0;
	// let timeId = setInterval(async () => {
	// 	$(".time").textContent = arr[index++];
	// 	if (index > arr.length) {
	// 		clearInterval(timeId);
	// 		$(".time").style.display = "none";
	// 		$(".mask-runing").style.display = "block";
	// 		s = await distant();
	// 		data = s.position;
	// 		alert(data);
	// 		p1[0] = data.KL;
	// 		p1[1] = data.kT;
	// 		startRun();
	// 	}
	// }, 1000);

	// 开始运动的函数
	// 计时索引
	let ms = 0;
	function startRun() {
		runId = setInterval(async () => {
			index1++;
			s = await distant();
			ms++;
			if (s.info === "SUCCESS") {
				data = s.position;
				p2[0] = data.KL;
				p2[1] = data.kT;
				dis = AMap.GeometryUtil.distance(p1, p2);
				p1[0] = p2[0];
				p1[1] = p2[1];
				meter += (dis / 1000).toFixed(3);
				calorie = (meter * 1.57).toFixed(2);
				$("#milesNum").textContent = meter;
				$("#paceNum").textContent = (dis / ms).toFixed(1);
				$("#calorieNum").textContent = calorie;
				ms = 0;
			}
			$("timeNum").textContent = formatTime(index1);
			// ss = getRandom(3, 7);
			// meter += ss;
			// calorie = (meter * 1.57).toFixed(2);
			// $("#paceNum").textContent = ss;
			// $("#milesNum").textContent = (meter / 1000).toFixed(3);
			// $("#calorieNum").textContent = calorie;
		}, 1000);
	}
	startRun();
	//数据的渲染

	//点击暂停效果的实现
	$("#stop").addEventListener("click", function () {
		$("#stopBtn").style.display = "none";
		$("#continueBtn").style.display = "block";
		clearInterval(runId);
	});
	// 继续效果的实现
	$("#continues").addEventListener("click", function () {
		startRun();
		$("#continues").style = "animation: run 0.2s linear forwards;";
		$("#end").style = "animation: move 0.2s linear forwards;";
		let timeId = setTimeout(() => {
			$("#continues").style = "";
			$("#end").style = "";
			$("#stopBtn").style.display = "block";
			$("#continueBtn").style.display = "none";
			clearTimeout(timeId);
		}, 190);
	});
	// 结束效果的实现
	$("#end").addEventListener("click", function () {
		clearInterval(runId);
		axios
			.post("/api/exercise/save", {
				type: 0,
				duration: index1,
				calorie,
				meter,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					location.href = "./sports_run.html";
				}
			});
	});
});
